package com.tushen.labor.adapter;

import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tushen.labor.R;
import com.tushen.labor.bean.Person;
import com.tushen.labor.util.ImageUtil;
import com.tushen.labor.view.ViewAdjust;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ItemAdapter extends BaseAdapter {

    private static final String TAG = "ItemAdapter";
    private LayoutInflater mInflater;
    private List<Person> mList = null;

    public ItemAdapter(LayoutInflater inflater) {
        mInflater = inflater;
    }

    @Override
    public int getCount() {

        if (mList == null) {
            //Log.d(TAG, "getCount mList == null ...size = 0");
            return 0;
        }
        //Log.d(TAG, "getCount size = " + mList.size());
        return mList.size();
    }

    public void clear() {
        mList = null;
    }

    public void setList(List<Person> list) {
        clear();
        if (list == null || list.size() <= 0) {
            notifyDataSetChanged();
            return;
        }
        mList = new ArrayList<>();
        mList.addAll(list);
        Log.d(TAG, "setList size = " + mList.size());
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public static  boolean checkBoundIsOk(Collection c, int index){

        if(c == null || index < 0 || index >= c.size()){
            return false;
        }
        return true;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (!checkBoundIsOk(mList, position)) {
            return null;
        }

        HolderView holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_item, parent, false);
            ViewAdjust.adjustLayoutParams(mInflater.getContext(), convertView.findViewById(R.id.inner_root_view));
            holder = new HolderView();
            holder.imageView = convertView.findViewById(R.id.item_image);
            holder.name = convertView.findViewById(R.id.item_text_name);
//            holder.companyName = convertView.findViewById(R.id.item_text_company);
//            holder.groupName = convertView.findViewById(R.id.item_text_group);
//            holder.posName = convertView.findViewById(R.id.item_text_pos);
            holder.inoutType = convertView.findViewById(R.id.item_text_inout_type);
            holder.inoutTime = convertView.findViewById(R.id.item_text_inout_time);
            convertView.setTag(holder);
        } else {
            holder = (HolderView) convertView.getTag();
        }

        //holder.imageView.setImageDrawable(new ColorDrawable(mColorList.get(position)));
        //Log.d(TAG,"getView pos =  "+position+" "+mList.get(position));

        Person person = mList.get(position);
        ImageUtil.getInstance().displayImage(mInflater.getContext(), person.getImageUrl(), holder.imageView, R.drawable.blank,true,ViewAdjust.getHeightPxAdjust(mInflater.getContext(),10));
        holder.name.setText(person.getName());
//        holder.companyName.setText(person.getCompanyName());
//        holder.groupName.setText(person.getGroupName());
//        holder.posName.setText(person.getPosName());
        holder.inoutType.setText(person.getInoutName());
        holder.inoutTime.setText(person.getInoutTime());

        return convertView;
    }


    private static class HolderView {
        //RoundedImageView
        ImageView imageView;

        TextView name;
        TextView companyName;
        TextView groupName;
        TextView posName;
        TextView inoutType;
        TextView inoutTime;
    }

}
